﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _22.中介者模式
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractCardPartner A = new ParterA();
            AbstractCardPartner B = new ParterB();

            A.MoneyCount = 20;// 初始钱
            B.MoneyCount = 20;

            AbstractMediator mediator = new MediatorPater(A, B);

            A.ChangeCount(5, mediator);// A赢了
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount);// 应该是25
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是15

            B.ChangeCount(10, mediator);// B 赢了
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount);// 应该是15
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是25
            Console.Read();

        }
    }
    public abstract class AbstractCardPartner// 抽象牌友类
    {
        public int MoneyCount { get; set; }
        protected AbstractCardPartner()
        {
            MoneyCount = 0;
        }
        public abstract void ChangeCount(int count, AbstractMediator mediator);
    }
    public class ParterA : AbstractCardPartner// 牌友A类
    {
        public override void ChangeCount(int count, AbstractMediator mediator)// 依赖与抽象中介者对象
        {
            mediator.AWin(count);
        }
    }

    public class ParterB : AbstractCardPartner// 牌友B类
    {
        public override void ChangeCount(int count, AbstractMediator mediator)// 依赖与抽象中介者对象
        {
            mediator.BWin(count);
        }
    }
    public abstract class AbstractMediator// 抽象中介者类
    {
        protected AbstractCardPartner A;
        protected AbstractCardPartner B;
        protected AbstractMediator(AbstractCardPartner a, AbstractCardPartner b)
        {
            A = a;
            B = b;
        }
        public abstract void AWin(int count);
        public abstract void BWin(int count);
    }
    public class MediatorPater : AbstractMediator// 具体中介者类
    {
        public MediatorPater(AbstractCardPartner a, AbstractCardPartner b) : base(a, b) { }
        public override void AWin(int count)
        {
            A.MoneyCount += count;
            B.MoneyCount -= count;
        }
        public override void BWin(int count)
        {
            B.MoneyCount += count;
            A.MoneyCount -= count;
        }
    }

}
